from  PyQt4.QtCore import QObject
from random import randint

from config import ROLES


class Zobrist:
    def __init__(self):
        self.size = 0
        self.autoplayer = []
        self.manplayer = []
        self.code = 0
        self.role = None

    def setup(self, size=15, role=ROLES.vacant):
        if size != 0:
            self.size = size
        else:
            self.size = 15
        self.autoplayer = []
        self.manplayer = []
        self.code = randint(0, 1e9)
        self.role = role
        for i in range(self.size ** 2):
            self.autoplayer.append(randint(0, 1e9))
            self.manplayer.append(randint(0, 1e9))

    def update(self, x, y, role):
        index = self.size * x + y
        if role == self.role:
            self.code ^= self.autoplayer[index]
        else:
            self.code ^= self.manplayer[index]
        return self.code
